<template>
	<view class="u-wrap">
		<!-- 公告栏 -->
		<view class="u-m-t-1">
			<u-cell-group>
				<u-cell-item @click="notice" icon="star" title="公告通知"></u-cell-item>
			</u-cell-group>
		</view>
		<!-- 轮播图 -->
		<swiper class="swiper" :indicator-color='indicatorColor' circular :indicator-dots="indicatorDots"
			:autoplay="autoplay" :interval="interval" :duration="duration">
			<swiper-item class="swiper-item" v-for="(item,index) in swipperList">
				<image @click="toDetail(item.goods)" class="imgs" :src="item.images.split(',')[0]"></image>
			</swiper-item>
		</swiper>
		<!-- 搜索 -->
		<view class="tab-sticky">
			<u-search @change="change"  v-model="parms.keywords" margin="10px" style="flex-grow: 1;" bg-color="#FFF" :show-action="true" action-text="搜索" :animation="true"></u-search>
		</view>
		<!-- 瀑布流 -->
		<view class="">
			<u-waterfall v-model="flowList" ref="uWaterfall1">
				<template v-slot:left="{leftList}">
					<view class="demo-warter" v-for="(item, index) in leftList" :key="index">
						<!-- 警告：微信小程序中需要hx2.8.11版本才支持在template中结合其他组件，比如下方的lazy-load组件 -->
						<u-lazy-load @click="toDetail(item)" threshold="-450" border-radius="10" :image="item.image" :index="index">
						</u-lazy-load>
						<view class="demo-title">
							{{item.title}}
						</view>
						<view class="demo-shop">
							{{item.introduce}}
						</view>
						<view class="demo-tag">
							<view v-if="item.type == '0'" class="demo-tag-owner">
								失物
							</view>
							<view v-else style="margin-left: 0;" class="demo-tag-text">
								拾物
							</view>
						</view>
					</view>
				</template>
				<template v-slot:right="{rightList}">
					<view class="demo-warter" v-for="(item, index) in rightList" :key="index">
						<u-lazy-load @click="toDetail(item)" threshold="-450" border-radius="10" :image="item.image" :index="index">
						</u-lazy-load>
						<view class="demo-title">
							{{item.title}}
						</view>
						<view class="demo-shop">
							{{item.introduce}}
						</view>
						<view class="demo-tag">
							<view v-if="item.type == '0'" class="demo-tag-owner">
								失物
							</view>
							<view style="margin-left: 0;" v-else class="demo-tag-text">
								拾物
							</view>
						</view>
					</view>
				</template>
			</u-waterfall>
			<u-loadmore bg-color="rgb(240, 240, 240)" :status="loadStatus" @loadmore="loadMore"></u-loadmore>
		</view>
	</view>
</template>

<script setup>
	import { onReady,onReachBottom } from '@dcloudio/uni-app';
import {
	reactive,
		ref
	} from 'vue';
	import {getIndexBannerApi,getIndexListApi} from '../../src/api/index.js'
	//颜色
	const indicatorColor = ref("#FFF")
	//是否显示控制板指示点
	const indicatorDots = ref(true)
	//是否自动播放
	const autoplay = ref(true)
	//自动切换时间间隔
	const interval = ref(2000)
	//滑动动画时长
	const duration = ref(500)
	//轮播图数据
	const swipperList = ref([])
	//瀑布流
	const flowList = ref([])
	//获取轮播图数据
	const getIndexBanner = async()=>{
		let res = await getIndexBannerApi()
		if(res && res.code == 200){
			console.log(res)
			swipperList.value = res.data;
		}
	}
	//获取热推数据
	const uWaterfall1 = ref()
	const loadStatus = ref('loadmore')
	const pages = ref(0)
	const parms = reactive({
		currentPage:1,
		pageSize:10,
		keywords:''
	})
	const getIndexList = async()=>{
		let res = await getIndexListApi(parms)
		if(res && res.code == 200){
			console.log(res)
			flowList.value = flowList.value.concat(res.data.records)
			pages.value = res.data.pages
		}
	}
	//加载更多
	const loadMore = ()=>{
		console.log('点击加载更多')
		//如果当前页大于总页数，说明没有数据了
		if(parms.currentPage >= pages.value){
			loadStatus.value = 'nomore'
			return;
		}
		loadStatus.value = 'loading'
		//页数加1
		parms.currentPage = ++parms.currentPage;
		//获取数据
		getIndexList()
	}
	//触底加载
	onReachBottom(()=>{
		console.log('触底加载')
		//如果当前页大于总页数，说明没有数据了
		if(parms.currentPage >= pages.value){
			loadStatus.value = 'nomore'
			return;
		}
		loadStatus.value = 'loading'
		//页数加1
		parms.currentPage = ++parms.currentPage;
		//获取数据
		getIndexList()
	})
	//搜索
	const change = ()=>{
		//清空瀑布流数据
		uWaterfall1.value.clear()
		parms.currentPage = 1;
		//获取数据
		getIndexList()
	}
	//跳转详情
	const toDetail = (item)=>{
		console.log(item)
		if(item.type == '0'){
			uni.navigateTo({
				url:"../find_detail/find_detail?goods="+JSON.stringify(item)
			})
		}else{
			uni.navigateTo({
				url:"../claim_detail/claim_detail?goods="+JSON.stringify(item)
			})
		}
	}
	//公告
	const notice = ()=>{
		uni.navigateTo({
			url:'../notice/notice'
		})
	}
	onReady(()=>{
		getIndexBanner()
		getIndexList()
	})
</script>

<style lang="scss">
	.swiper {
		height: 300rpx;
	}

	.swiper-item {
		display: block;
		height: 300rpx;
		line-height: 300rpx;
		text-align: center;

		.imgs {
			height: 300rpx;
			width: 100%;
		}
	}

	.demo-warter {
		border-radius: 8px;
		margin: 5px;
		background-color: #ffffff;
		padding: 8px;
		position: relative;
	}

	.u-close {
		position: absolute;
		top: 32rpx;
		right: 32rpx;
	}

	.demo-image {
		width: 100%;
		border-radius: 4px;
	}

	.demo-title {
		font-size: 30rpx;
		margin-top: 5px;
		color: $u-main-color;
	}

	.demo-tag {
		display: flex;
		margin-top: 5px;
	}

	.demo-tag-owner {
		background-color: $u-type-error;
		color: #FFFFFF;
		display: flex;
		align-items: center;
		padding: 4rpx 14rpx;
		border-radius: 50rpx;
		font-size: 20rpx;
		line-height: 1;
	}

	.demo-tag-text {
		border: 1px solid $u-type-primary;
		color: $u-type-primary;
		margin-left: 10px;
		border-radius: 50rpx;
		line-height: 1;
		padding: 4rpx 14rpx;
		display: flex;
		align-items: center;
		border-radius: 50rpx;
		font-size: 20rpx;
	}

	.demo-price {
		font-size: 30rpx;
		color: $u-type-error;
		margin-top: 5px;
	}

	.demo-shop {
		font-size: 22rpx;
		color: $u-tips-color;
		margin-top: 5px;
	}
	.tab-sticky{
		position: sticky;
		z-index: 99;
		top: 0;
		left: 0;
		background-color: #f2f2f2;
		display: flex;
		align-items: center;
	}
</style>
